﻿using System;
using System.Net;
using System.Windows;

namespace FlickrWP7.Core
{
    public static class Groups
    {
        private static HelperDelegate helperDelegateInstance;

        public static string LastGroupResult { get; set; }

        public enum Language
        {
            English,
            German,
            Spanish,
            French,
            Italian,
            Korean,
            Portuguese,
            TraditionalChinese
        }

        public static void GetList(string apiKey, string groupID, Language language, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.getInfo&api_key={0}&format=json&group_id={1}&lang=",
                    apiKey,groupID);

            switch (language)
            {
                case Language.English:
                    {
                        URL += "en-us";
                        break;
                    }
                case Language.German:
                    {
                        URL += "de-de";
                        break;
                    }
                case Language.Spanish:
                    {
                        URL += "es-us";
                        break;
                    }
                case Language.French:
                    {
                        URL += "fr-fr";
                        break;
                    }
                case Language.Italian:
                    {
                        URL += "it-it";
                        break;
                    }
                case Language.Korean:
                    {
                        URL += "ko-kr";
                        break;
                    }
                case Language.Portuguese:
                    {
                        URL += "pt-br";
                        break;
                    }
                case Language.TraditionalChinese:
                    {
                        URL += "zh-hk";
                        break;
                    }
            }

            client.DownloadStringAsync(new Uri(URL));
        }

        public static void Search(string apiKey, string text, HelperDelegate helperDelegate,string page ="",string perPage="")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.search&api_key={0}&format=json&text={1}",
                    apiKey,text);

            if (page != "")
                URL += "&page=" + page;
            if (perPage != "")
                URL += "&per_page=" + perPage;

            client.DownloadStringAsync(new Uri(URL));
        }

        public static void Browse(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, string catID = "")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.browse&api_key={0}&format=json&auth_token={1}&api_sig={2}",
                    apiKey, authToken,signature);

            if (catID != "")
                URL += "&cat_id=" + catID;

            client.DownloadStringAsync(new Uri(URL));
        }

        public static class Members
        {
            public static class SelectedTypes
            {
                public const string All = "2,3,4";
                public const string Regular = "2";
                public const string Mod = "3";
                public const string Admin = "4";
                public const string RegularAndMod = "2,3";
                public const string RegularAndAdmin = "2,4";
                public const string ModAndAdmin = "3,4";
            }

            public static void GetList(string apiKey,string authToken, string signature, string groupID, string types, HelperDelegate helperDelegate, 
                string page="", string perPage ="")
            {

                helperDelegateInstance = helperDelegate;

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Core.Groups.client_DownloadStringCompleted);
                string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.members.getList&api_key={0}&format=json&auth_token={1}&api_sig={2}&membertypes={3}&group_id={4}",
                        apiKey, authToken, signature,types,groupID);

                if (page != "")
                    URL += "&page=" + page;
                if (perPage != "")
                    URL += "&per_page=" + perPage;

                client.DownloadStringAsync(new Uri(URL));
            }
        }

        public static class Pools
        {
            static void Add(string apiKey, string authToken, string signature, string groupID, string photoID, HelperDelegate helperDelegate)
            {
                helperDelegateInstance = helperDelegate;

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Core.Groups.client_DownloadStringCompleted);
                string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.pools.add&api_key={0}&format=json&auth_token={1}&api_sig={2}&photo_id={3}&group_id={4}",
                        apiKey, authToken, signature, photoID, groupID);

                client.DownloadStringAsync(new Uri(URL));
            }

            static void GetContext(string apiKey, string authToken, string signature, string groupID, string photoID, HelperDelegate helperDelegate)
            {
                helperDelegateInstance = helperDelegate;

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Core.Groups.client_DownloadStringCompleted);
                string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.pools.getContext&api_key={0}&format=json&auth_token={1}&api_sig={2}&photo_id={3}&group_id={4}",
                        apiKey, authToken, signature, photoID, groupID);

                client.DownloadStringAsync(new Uri(URL));
            }

            static void GetGroups(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, string page="",string perPage="")
            {
                helperDelegateInstance = helperDelegate;

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Core.Groups.client_DownloadStringCompleted);
                string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.pools.getGroups&api_key={0}&format=json&auth_token={1}&api_sig={2}",
                        apiKey, authToken, signature);

                if (page != "")
                    URL += "&page=" + page;
                if (perPage != "")
                    URL += "&per_page=" + perPage;

                client.DownloadStringAsync(new Uri(URL));
            }

            public static void GetPhotos(string apiKey, string groupID, HelperDelegate helperDelegate, string[] extras,
            string[] tags, string userID = "", string page = "",
            string perPage = "", string authToken="", string signature ="")
            {
                helperDelegateInstance = helperDelegate;

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&api_key={0}&group_id={1}&format=json",
                        apiKey,groupID);

                if (tags != null)
                {
                    string toAdd = "&tags=";
                    for (int i = 0; i < tags.Length; i++)
                    {
                        if (i != tags.Length - 1)
                            toAdd += tags[i] + ",";
                        else
                            toAdd += tags[i];
                    }
                    URL += toAdd;
                }

                if (extras != null)
                {
                    string toAdd = "&extras=";
                    for (int i = 0; i < extras.Length; i++)
                    {
                        if (i != extras.Length - 1)
                            toAdd += extras[i] + ",";
                        else
                            toAdd += extras[i];
                    }
                    URL += toAdd;
                }

                if (userID != "")
                    URL += "&user_id=" + userID;
                if (page != "")
                    URL += "&page=" + page;
                if (perPage != "")
                    URL += "&per_page=" + perPage;

                client.DownloadStringAsync(new Uri(URL));
            }

            static void Remove(string apiKey, string authToken, string signature, string groupID, string photoID, HelperDelegate helperDelegate)
            {
                helperDelegateInstance = helperDelegate;

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Core.Groups.client_DownloadStringCompleted);
                string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.pools.remove&api_key={0}&format=json&auth_token={1}&api_sig={2}&photo_id={3}&group_id={4}",
                        apiKey, authToken, signature, photoID, groupID);

                client.DownloadStringAsync(new Uri(URL));
            }
        }

        static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            LastGroupResult = e.Result;
            helperDelegateInstance();
        }
    }
}
